# -*- coding: utf-8 -*-
# Created by shu2015626 on 2019-7-11
"""
写完了，不怎么会去修改代码的任务。可以定义在这里。
否则建议，通过动态的方式进行配置。
这里写这个例子。是为了使应用看起来完整。而且爬虫类应用一般这样做。
"""
import logging
from celery import shared_task
from apps.CeleryTaskBase import CeleryTaskBase

from common_tasks.business.b_update_tradecal import BusinessUpdateTradeCal
from common_tasks.business.b_heartbeat import BusinessHeatbeat

logger = logging.getLogger("tasks")


@shared_task(bind=True, name='common.exec_update_tradecal', base=CeleryTaskBase, max_retries=3,
             default_retry_delay=10 * 60, retry_kwargs={'max_retries': 3, 'countdown': 10 * 60})
def exec_update_tradecal(self):
    res = {}
    # print("self: ", self)
    # print("dir(self): ", dir(self))
    # print("self.request: ", self.request)
    # print("dir(self.request): ", dir(self.request))
    # print("self.app: ", self.app)
    # print("dir(self.app): ", dir(self.app))
    # print("self.s: ", self.s())
    # print("dir(self.s): ", dir(self.s))
    obj = BusinessUpdateTradeCal()
    result = obj.run()
    res['result'] = result
    res['hostname'] = self.request.hostname
    res['task_name'] = self.request.task
    res['task_id'] = self.request.id
    # print('res: ', res)
    return res


@shared_task(bind=True, name='common.exec_heartbeat')
def exec_heartbeat(self):
    """
    这个任务执行频率很高，就不绑定base=CeleryTaskBase了。
    否则还得每天去清理数据库内容。
    也不需要捕获错误和重试。因为会有另外的进程来监控heartbeat
    """
    res = {}
    obj = BusinessHeatbeat()
    result = obj.run()
    res['result'] = result
    res['hostname'] = self.request.hostname
    res['task_name'] = self.request.task
    res['task_id'] = self.request.id
    return res
